package com.javandad.app.shared.entity;

import java.util.Date;
import java.util.List;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Unique;

import com.javandad.app.shared.entity.search.SearchResult;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "false")
public class User implements java.io.Serializable, SearchResult, Mailable {

	private static final long serialVersionUID = 9158765280966086812L;
	
	@SuppressWarnings("unused")
	private User(){}
	public User(String username, String firstName, String lastName,
			Date creationDate, String email, String cellNo, String phoneNo,
			String country, String city, String postalAddress) {
		super();
		this.username = username;
		this.firstName = firstName;
		this.lastName = lastName;
		this.creationDate = creationDate;
		this.email = email;
		this.cellNo = cellNo;
		this.phoneNo = phoneNo;
		this.country = country;
		this.city = city;
		this.postalAddress = postalAddress;
	}
	public User(String username, User copy) {
		super();
		this.username = username;
		this.firstName = copy.firstName;
		this.lastName = copy.lastName;
		this.creationDate = copy.creationDate;
		this.email = copy.email;
		this.cellNo = copy.cellNo;
		this.phoneNo = copy.phoneNo;
		this.country = copy.country;
		this.city = copy.city;
		this.postalAddress = copy.postalAddress;
	}
	
	public User(String userId, String email) {
		this.userId = userId;
		this.email = email;
		this.username = null;
	}
	public String getUserId()
	{
		return userId;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username)
	{
		this.username = username;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public Date getCreationDate() {
		return creationDate;
	}
	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getCellNo() {
		return cellNo;
	}
	public void setCellNo(String cellNo) {
		this.cellNo = cellNo;
	}
	public String getPhoneNo() {
		return phoneNo;
	}
	public void setPhoneNo(String phoneNo) {
		this.phoneNo = phoneNo;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getPostalAddress() {
		return postalAddress;
	}
	public void setPostalAddress(String postalAddress) {
		this.postalAddress = postalAddress;
	}
	public List<Membership> getGroupMemberships()
	{
		return this.groupMemberships;
	}
	public Membership getGroupMembership(UserGroup group)
	{
		for(Membership m : groupMemberships)
			if(m.getUserGroup().equals(group.getGroupURL()))
				return m;
		return null;
	}
	public Date getLastChatPoll() {
		return lastChatPoll;
	}
	public void setLastChatPoll(Date lastChatPoll) {
		this.lastChatPoll = lastChatPoll;
	}
	
	public String getEncodedKey() {
		return encodedKey;
	}
	public String toString(){
		return firstName + " " + lastName;  
	}
	
	
	
	


	public Long getProfileFodo() {
		return profileFodo;
	}
	public void setProfileFodo(Long profileFodo) {
		this.profileFodo = profileFodo;
	}






	public String getProfileFodoURL() {
		return profileFodoURL;
	}
	public void setProfileFodoURL(String profileFodoURL) {
		this.profileFodoURL = profileFodoURL;
	}






	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String encodedKey;
	@Persistent
	@Extension(vendorName="datanucleus", key="gae.pk-name", value="true")
	private String userId;
	
	@Persistent
	@Unique
	private String username;
	
	@Persistent
	private String firstName;
	@Persistent
	private String lastName;
	@Persistent
	private Date creationDate;
	@Persistent
	private String email;
	@Persistent
	private String cellNo;
	@Persistent
	private String phoneNo;
	@Persistent
	private String country;
	@Persistent
	private String city;
	@Persistent
	private String postalAddress;
	
	private Long profileFodo = null;
	
	private String profileFodoURL = "/img/profile_default_pic.png";
	
	@Persistent(mappedBy = "member")
	transient
	private List<Membership> groupMemberships;
	@Persistent
	private Date lastChatPoll = null;
	@Override
	public Type getSearchResultType() {
		return SearchResult.Type.User;
	}
}
